package com.audaque.jadmin.system.shiro;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;

/**
 * @Description :
 * @FileName: AbstractAccessControlFilter.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年10月26日 下午2:53:09
 * @Version:V1.00
 */
public abstract class AbstractAccessControlFilter extends AccessControlFilter {

	protected final PatternMatcher patternMatcher = new AntPathMatcher();

	public List<String> patterns;

	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
			throws Exception {

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		return _isAccessAllowed(httpRequest, httpResponse, mappedValue);
	}

	protected abstract boolean _isAccessAllowed(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
			Object mappedValue) throws Exception;

	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		return _onAccessDenied(httpRequest, httpResponse);
	}

	protected abstract boolean _onAccessDenied(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
			throws Exception;

	/**
	 * 检查当前URI是否在指定URI中
	 * 
	 * @param uri
	 * @return
	 */
	public final boolean isMatch(String uri) {
		if (patterns == null) {
			return false;
		}
		for (String pattern : patterns) {
			if (pattern == null)
				continue;
			if (patternMatcher.matches(pattern, uri)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 得到当前访问的URI
	 * 
	 * @param request
	 * @return
	 */
	public final String getUri(HttpServletRequest request) {
		return WebUtils.getPathWithinApplication(WebUtils.toHttp(request));
	}

	public final boolean isAjaxRequest(HttpServletRequest request) {
		Enumeration<?> names = request.getHeaderNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			if (StringUtils.equalsIgnoreCase("X-Requested-With", name)) {
				String value = request.getHeader(name);
				return StringUtils.equalsIgnoreCase("XMLHttpRequest", value);
			}
		}
		return false;
	}

	public final boolean isAjax(HttpServletRequest request) {
		boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
		String ajaxFlag = null == request.getParameter("ajax") ? "false" : request.getParameter("ajax");
		boolean isAjax = ajax || ajaxFlag.equalsIgnoreCase("true");
		return isAjax;
	}

	public List<String> getPatterns() {
		return patterns;
	}

	public void setPatterns(List<String> patterns) {
		this.patterns = patterns;
	}

}
